import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../stores/auth'

// 页面组件导入
const Login = () => import('../pages/Login.vue')
const AdminLayout = () => import('../pages/AdminLayout.vue')
const Dashboard = () => import('../pages/Dashboard.vue')
const MovieManage = () => import('../pages/movies/MovieManage.vue')
const ActorManage = () => import('../pages/actors/ActorManage.vue')
const TagManage = () => import('../pages/tags/TagManage.vue')
const CommentReview = () => import('../pages/reviews/CommentReview.vue')
const DataAnalysis = () => import('../pages/statistics/DataAnalysis.vue')
const RoleManage = () => import('../pages/permissions/RoleManage.vue')
const CategoryManage = () => import('../pages/categories/CategoryManage.vue')
const UserManage = () => import('../pages/users/UserManage.vue')
const SystemUserManage = () => import('../pages/users/SystemUserManage.vue')
const Settings = () => import('../pages/Settings.vue')
const NewsManage = () => import('../pages/news/NewsManage.vue')

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/',
    component: AdminLayout,
    redirect: '/dashboard',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: { title: '仪表盘' }
      },
      {
        path: 'movies',
        name: 'MovieManage',
        component: MovieManage,
        meta: { title: '影片管理' }
      },
      {
        path: 'actors',
        name: 'ActorManage',
        component: ActorManage,
        meta: { title: '演员管理' }
      },
      {
        path: 'actors',
        name: 'ActorManage',
        component: ActorManage,
        meta: { title: '演员管理' }
      },
      {
        path: 'tags',
        name: 'TagManage',
        component: TagManage,
        meta: { title: '标签分类' }
      },
      {
        path: 'reviews',
        name: 'CommentReview',
        component: CommentReview,
        meta: { title: '评论审核' }
      },
      {
        path: 'statistics',
        name: 'DataAnalysis',
        component: DataAnalysis,
        meta: { title: '数据统计' }
      },
      {
        path: 'permissions',
        name: 'RoleManage',
        component: RoleManage,
        meta: { title: '权限管理' }
      },
      {
        path: 'categories',
        name: 'Categories',
        component: CategoryManage,
        meta: { title: '分类管理' }
      },

      {
        path: 'users',
        name: 'Users',
        component: UserManage,
        meta: { title: '前端用户管理' }
      },
      {
        path: 'system-users',
        name: 'SystemUsers',
        component: SystemUserManage,
        meta: { title: '系统用户管理' }
      },
      {
        path: 'news',
        name: 'NewsManage',
        component: NewsManage,
        meta: { title: '资讯管理' }
      },
      {
        path: 'settings',
        name: 'Settings',
        component: Settings,
        meta: { title: '系统设置' }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/dashboard'
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  const token = localStorage.getItem('cms_token')
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - CMS管理系统`
  }
  
  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    if (!token) {
      // 没有token，跳转到登录页
      next('/login')
    } else {
      // 有token，继续访问
      next()
    }
  } else {
    // 不需要认证的页面
    if (to.path === '/login' && token) {
      // 已登录但访问登录页，跳转到首页
      next('/dashboard')
    } else {
      next()
    }
  }
})

export default router